package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.AttendanceDedItemAddDTO;
import org.jsola.hr.dto.AttendanceDedItemUpdateDTO;
import org.jsola.hr.query.AttendanceDedItemQuery;
import org.jsola.hr.service.IAttendanceDedItemService;
import org.jsola.hr.vo.AttendanceDedItemListVO;
import org.jsola.hr.vo.AttendanceDedItemVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * @author zhr
 */
@RestController("hrAttendanceDedItemController")
@RequestMapping(HrConstants.API_PATH + "/v1/attendanceDedItems")
public class AttendanceDedItemController implements IAttendanceDedItemControllerAPI {

    @Autowired
    private IAttendanceDedItemService attendanceDedItemService;

    @Override
    @PostMapping
    public Result<AttendanceDedItemVO> save(@Valid @RequestBody AttendanceDedItemAddDTO attendanceDedItemAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增考勤扣款设置子项权限校验

        return Result.success(
                attendanceDedItemService.save(attendanceDedItemAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody AttendanceDedItemUpdateDTO attendanceDedItemUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改考勤扣款设置子项权限校验

        int count = attendanceDedItemService.update(attendanceDedItemUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{attendanceDedItemId}")
    public Result<Integer> delete(@PathVariable String attendanceDedItemId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除考勤扣款设置子项权限校验

        int count = attendanceDedItemService.deleteByIds(tokenUser, attendanceDedItemId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] attendanceDedItemIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除考勤扣款设置子项权限校验

        int count = attendanceDedItemService.deleteByIds(tokenUser, attendanceDedItemIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{attendanceDedItemId}")
    public Result<AttendanceDedItemVO> findById(@PathVariable String attendanceDedItemId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看考勤扣款设置子项权限校验

        return Result.success(
            attendanceDedItemService.selectById(attendanceDedItemId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<AttendanceDedItemListVO>> listAll(@Valid AttendanceDedItemQuery attendanceDedItemQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看考勤扣款设置子项权限校验

        return Result.success(
            attendanceDedItemService.select(attendanceDedItemQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<AttendanceDedItemListVO>> page(@Valid AttendanceDedItemQuery attendanceDedItemQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            attendanceDedItemService.selectPage(attendanceDedItemQuery, tokenUser.getSiteId())
        );
    }


}

